<?php 

class Bulker {

	protected $mysqliObj;
	protected $url;
	protected $dbname;
	protected $username;
	protected $password;

	function __construct($url, $dbname, $username, $password) {
		$this->url = $url;
		$this->dbname = $dbname;
		$this->username = $username;
		$this->password = $password;
	}

	function connect() {
		$this->mysqliObj = new mysqli($this->url, $this->username, $this->password, $this->dbname);
		if ($this->mysqliObj->connect_errno) {
			echo "Failed to connect to MySQL: (" . $this->mysqliObj->connect_errno . ") " . $this->mysqliObj->connect_error;
			return false;
		}
		return true;
	}
	
	function generateRandomString($length = 10) {
		$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
		$randomString = '';
		for ($i = 0; $i < $length; $i++) {
			$randomString .= $characters[rand(0, strlen($characters) - 1)];
		}
    return $randomString;
}
	
	function insertCategories() {
		$i=0;
		for ($i=0 ; $i<4 ; $i++) {
			$cat = $this->generateRandomString();
			if (!$this->mysqliObj->query("INSERT INTO categories (`id`, `category`) VALUES ('".($i+1)."', '".$cat."')")) {
			}
		}
	}
	
	function insertActors() {
		$i=0;
		for ($i=0 ; $i<400 ; $i++) {
			$id = $i + 1;
			$name = $this->generateRandomString(7 + rand(0, 6));
			$surname = $this->generateRandomString(7 + rand(0, 6));
			$this->mysqliObj->query("INSERT INTO actors (`id`, `firstName`, `lastName`) VALUES ('".$id."', '".$name."', '".$surname."')");
		}
	}
	
	function insertDirectors() {
		$i=0;
		for ($i=0 ; $i<6 ; $i++) {
			$id = $i + 1;
			$name = $this->generateRandomString(7 + rand(0, 6));
			$surname = $this->generateRandomString(7 + rand(0, 6));
			$this->mysqliObj->query("INSERT INTO directors (`id`, `firstName`, `lastName`) VALUES ('".$id."', '".$name."', '".$surname."')");
		}
	}
	
	function insertMovies() {
		$i=0;
		for ($i=0 ; $i<150 ; $i++) {
			$id = $i + 1;
			$title = $this->generateRandomString(7 + rand(0, 6));
			$yearOfRelease = rand(1960, 1960 + rand(0, 55));
			$directorID = rand(1, 6);
			$rating = 'G';
			$noCopies = rand(50, 50 + rand(0, 50));
			$price = rand(1, 100);
			if (!$this->mysqliObj->query("INSERT INTO movies (`id`, `title`,`yearOfRelease`,`directorID`,`rating`,`noCopies`,`price`) VALUES ('".$id."', '".$title."', '".$yearOfRelease."', '".$directorID."', '".$rating."', '".$noCopies."', '".$price."')")) {
				//skipping non unique
			}
		}
	}
	
	function insertHasCategory() {
		$i=0;
		for ($i=0 ; $i<150 ; $i++) {
			$id = $i + 1;
			$m = $i + 1;
			$cat = rand(1, 1+rand(0, 4));
			if (!$this->mysqliObj->query("INSERT INTO hascategory (`id`, `m-id`,`idCategories`) VALUES ('".$id."', '".$m."', '".$cat."')")) {
				//skipping non-unique
			}
		}
	}
	
	function insertHasActors() {
		$i=0;
		for ($i=0 ; $i<1500 ; $i++) {
			$id = $i + 1;
			$m = rand(1, rand(0, 4));
			$cat = rand(1, 1+rand(0, 400));
			$maj = rand(0, 100) % 2;
			if (!$this->mysqliObj->query("INSERT INTO hasactors (`id`, `m-id`,`idActors`, `isMajor`) VALUES ('".$id."', '".$m."', '".$cat."', '".$maj."')")) {
				//skipping non-unique
			}
		}
	}
	
	function insertMembers() {
		$i = 0;
		for ($i=0 ; $i<100 ; $i++) {
			$id = $i + 1;
			$fname = $this->generateRandomString(7 + rand(0, 6));
			$lname = $this->generateRandomString(7 + rand(0, 6));
			$uname = $this->generateRandomString(7 + rand(0, 6));
			$password = md5('1234');
			$ctype = $this->generateRandomString(7 + rand(0, 6));
			$cnumber = $this->generateRandomString(7 + rand(0, 6));
			$address = $this->generateRandomString(7 + rand(0, 6));
			$pNumber = $this->generateRandomString(7 + rand(0, 6));
			$storeManager = 0;
			if ($i%2 == 0) {
				$storeManager = 1;
			}
			if (!$this->mysqliObj->query("INSERT INTO members (`id`, `firstName`,`lastName`,`loginName`,`password`,`creditType`,`creditNumber`,`address`,`phoneNumber`,`storeManager`)
			VALUES ('".$id."', '".$fname."', '".$lname."', '".$uname."', '".$password."', '".$ctype."', '".$cnumber."', '".$address."', '".$pNumber."', '".$storeManager."')")) {
			echo "INSERT INTO members (`id`, `firstName`,`lastName`,`loginName`,`password`,`creditType`,`creditNumber`,`address`,`phoneNumber`,`storeManager`)
			('".$id."', '".$fname."', '".$lname."', '".$uname."', '".$password."', '".$ctype."', '".$cnumber."', '".$address."', '".$pNumber."', '".$storeManager."')";
			}
		}
	}
	
	function insertOrders() {
		$count = 1;
		for ($i=0 ; $i<100 ; $i++) {
			$numOrders = rand(1, 5);
			$orderId = $this->generateRandomString(25);
			$shipping = $this->generateRandomString(10);
			$idMember = $i+1;
			for ($j=0 ; $j < $numOrders ; $j++) {
				$id = $count;
				$mId = rand(1, 798);
				$price = rand(1, 99);
				$noCopies = rand(1, 4);
				$date = date('Y-m-d', strtotime( '+'.mt_rand(0,30).' days'));
				if (!$this->mysqliObj->query("INSERT INTO orders (`id`, `orderId`,`idMember`, `m-id`, `shipping`, `price`, `noCopies`, `date`) 
					VALUES ('".$id."', '".$orderId."', '".$idMember."', '".$mId."', '".$shipping."', '".$price."', '".$noCopies."', '".$date."')")) {
				//skipping non-unique
				}
				$count++;
			}
		}
	}

}

$b = new Bulker("localhost", "mde515", "root", "");
$b->connect();
/*$b->insertCategories();*/
//$b->insertActors();
//$b->insertDirectors();
//$b->insertMovies();
//$b->insertHasCategory();
//$b->insertHasActors();

//$b->insertMembers();
$b->insertOrders();